package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.entity.ProductAttrValueEntity;
import com.atguigu.gulimall.product.service.*;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrsVo;
import com.atguigu.gulimall.product.vo.SkuItemVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrService attrService;
    @Autowired
    AttrGroupService attrGroupService;
    @Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    ProductAttrValueService productAttrValueService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");//拿到模糊查询的key
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        if (!StringUtils.isEmpty(key)) {
            //如果查询条件存在，则加入查询条件
            wrapper.and((obj) -> {
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        if (catelogId == 0) {
            //如果catelogId为 0 ，则查询所有
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        } else {
            wrapper.eq("catelog_id", catelogId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        }

    }

    /**
     * //1.查出当前分类下的所有属性分组
     * //2.查出每个属性分组的所有属性
     *
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        //1.查出当前分类下的所有属性分组
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        //2.查出每个属性分组的所有属性
        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map((attrGroupEntity) -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(attrGroupEntity, attrGroupWithAttrsVo);
            List<AttrEntity> attrs = attrService.getRelationAttr(attrGroupEntity.getAttrGroupId());
            attrGroupWithAttrsVo.setAttrs(attrs);
            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<SkuItemVo.SpuItemGroupAttrVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
//        class SpuItemGroupAttrVo{
//            private String groupName;
//            List<SkuItemVo.SpuBaseAttrVo> baseAttrVos;
//        }
//        class SpuBaseAttrVo{
//            private String attrName;
//            private String attrValue;
//        }
        //1.通过catalogId查出attr_group的集合
        List<AttrGroupEntity> groupList = attrGroupService.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catalogId));

        List<SkuItemVo.SpuItemGroupAttrVo> collect = groupList.stream().map(group -> {
            SkuItemVo.SpuItemGroupAttrVo spuItemGroupAttrVo = new SkuItemVo.SpuItemGroupAttrVo();
            spuItemGroupAttrVo.setGroupName(group.getAttrGroupName());
            //对于每一个   属性分组  在pms_attr_attr_ group_relation 遍历他拥有的属性  获得子属性集合
            List<AttrAttrgroupRelationEntity> relationList = attrAttrgroupRelationService.list(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", group.getAttrGroupId()));
            //对于每一个子属性集合  在pms_product_attr_value中 遍历获得他的值
            List<SkuItemVo.SpuBaseAttrVo> spuBaseAttrVos = relationList.stream().map(relation -> {
                BaseMapper<ProductAttrValueEntity> baseMapper = productAttrValueService.getBaseMapper();
                ProductAttrValueEntity entity = baseMapper.selectOne(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId).eq("attr_id", relation.getAttrId()));
                SkuItemVo.SpuBaseAttrVo spuBaseAttrVo = new SkuItemVo.SpuBaseAttrVo();
                spuBaseAttrVo.setAttrName(entity.getAttrName());
                spuBaseAttrVo.setAttrValue(entity.getAttrValue());
                return spuBaseAttrVo;
            }).collect(Collectors.toList());
            spuItemGroupAttrVo.setBaseAttrVos(spuBaseAttrVos);
            return spuItemGroupAttrVo;
        }).collect(Collectors.toList());
        return collect;
    }

}